package cn.caishen.report.common.utils;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

/**
 * @author LB
 */
public class DateTimeConverter implements Converter {

    public DateTimeConverter() {

    }

    @Override
    public <T> T convert(Class<T> type, Object value) {
        return (T) toDate(type, value);
    }

    public static <T> Object toDate(Class<T> type, Object value) {
        if (value == null || "".equals(value)) {
            return null;
        }
        Date date;
        try {
            String sDate = value.toString();
            StringBuffer datePattern = new StringBuffer();
            if (sDate != null) {
                StringTokenizer st = new StringTokenizer(sDate, "/-. :");
                int count = 0;
                int year = 0;
                int month = 0;
                int day = 0;
                int hour = 0;
                int minute = 0;
                int second = 0;
                char delim = '\0';
                int length = sDate.length();
                int lengthPattern = 0;
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if (count == 0) {
                        year = token.length();
                        for (int i = 0; i < year; i++) {
                            datePattern.append("y");
                        }

                        lengthPattern += year;
                    } else if (count == 1) {
                        month = token.length();
                        for (int i = 0; i < month; i++) {
                            datePattern.append("M");
                        }

                        lengthPattern += month;
                    } else if (count == 2) {
                        day = token.length();
                        for (int i = 0; i < day; i++) {
                            datePattern.append("d");
                        }

                        lengthPattern += day;
                    } else if (count == 3) {
                        hour = token.length();
                        for (int i = 0; i < hour; i++) {
                            datePattern.append("H");
                        }

                        lengthPattern += hour;
                    } else if (count == 4) {
                        minute = token.length();
                        for (int i = 0; i < minute; i++) {
                            datePattern.append("m");
                        }

                        lengthPattern += minute;
                    } else if (count == 5) {
                        second = token.length();
                        for (int i = 0; i < second; i++) {
                            datePattern.append("s");
                        }

                        lengthPattern += second;
                    }

                    if (lengthPattern < length) {
                        delim = sDate.charAt(lengthPattern);
                        datePattern.append(delim);
                        lengthPattern++;
                    }
                    count++;
                }

                if (count < 2 || datePattern.length() == 0) {
                    datePattern = new StringBuffer();
                    datePattern.append("yyyyMMdd");
                }
            }
            date = (new SimpleDateFormat(datePattern.toString())).parse(sDate);
            if ("java.sql.Date".equals(type.getName())) {
                return new Date(date.getTime());
            }
        } catch (Exception e) {
            throw new ConversionException(e);
        }
        if ("java.sql.Timestamp".equals(type.getName())) {
            return new Timestamp(date.getTime());
        }
        return date;
    }


}
